<div class="row">
    <div class="col-md-2">
        <ul class="nav nav-pills nav-stacked">
            <li><?php echo $this->Html->link('Trang chủ', array('action' => 'index')); ?></li>
            <li class="active"><?php echo $this->Html->link('Hôm nay', array('action' => 'today')); ?></li>
            <li><?php echo $this->Html->link('Top Comment', array('action' => 'topcomment')); ?></li>
            <li><?php echo $this->Html->link('Danh sách đã nghe trong tháng', array('action' => 'listened')); ?></li>
        </ul>
    </div>
    <div class="col-md-10">
        <table class="table table-bordered">
            <tr class="text-success">
                <th class="text-center">STT</th>
                <th class="text-center">Người nhận</th>
                <th class="text-center">Bài hát</th>
                <th class="text-center">Lời nhắn</th>
                <th class="text-center">Người gửi</th>
                <th class="text-center">Thao tác</th>
            </tr>
            <?php foreach ($orders as $order): ?>
                <?php if ($order['Order']['status'] == 1) : ?>
                    <tr class="choose-song text-muted">
                        <td><?php echo $order['Order']['id'] ?></td>
                        <td><?php echo h($order['Order']['receiver']) ?></td>
                        <td><?php echo $this->Html->link(h($order['Order']['song']), h($order['Order']['link']), array('target' => '_blank')); ?></td>
                        <td class="col-md-4">
                            <em>
                                <?php
                                if (strlen($order['Order']['message']) > 100) {
                                    echo h(substr($order['Order']['message'], 0, 100)) . '...';
                                } else {
                                    echo h($order['Order']['message']);
                                }
                                ?>
                            </em>
                        </td>
                        <td class="col-md-2"><?php echo h($order['Order']['sender']) ?></td>
                        <td class="col-md-2 text-center">
                            <span class="unchoose" title="Bỏ chọn"
                                    value="<?php echo h($order['Order']['id']); ?>">
                            </span>
                            <?php echo $this->Form->postLink('', array('action' => 'delete', $order['Order']['id']), array('title'=>"Xóa",'class' => 'delete'), __('Bạn có muốn xóa bài hát: %s?', h($order['Order']['song']))); ?>
                        </td>
                    </tr>
                <?php else: ?>
                    <tr>
                        <td><?php echo $order['Order']['id'] ?></td>
                        <td><?php echo h($order['Order']['receiver']) ?></td>
                        <td><?php echo $this->Html->link(h($order['Order']['song']), h($order['Order']['link'])); ?></td>
                        <td class="col-md-4"><em><?php
                                if (strlen($order['Order']['message']) > 200) {
                                    echo h(substr($order['Order']['message'], 0, 160)) . '...';
                                } else {
                                    echo h($order['Order']['message']);
                                }
                                ?></em>
                        </td>
                        <td class="col-md-2"><?php echo h($order['Order']['sender']) ?></td>
                        <td class="col-md-2 text-center">
                            <span class="choose" title="Chọn" value="<?php echo $order['Order']['id']; ?>"><span
                                    class="hidden"><?php echo h($order['Order']['link']); ?></span></span>
                            <?php echo $this->Form->postLink('', array('action' => 'delete', $order['Order']['id']), array('title'=>"Xóa",'class' => 'delete'), __('Bạn có muốn xóa bài hát: %s?', h($order['Order']['song']))); ?>
                        </td>
                    </tr>

                <?php endif; ?>
            <?php endforeach; ?>
        </table>
        <?php if(count($orders) > 15): ?>
            <ul class="pagination pagination-sm pull-right">
                <?php
                echo '<li class="disabled">' . $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled')) . '</li>';
                echo '<li>' . $this->Paginator->numbers(array('separator' => '')) . '</li>';
                echo '<li class="disabled">' . $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled')) . '</li>';
                ?>
            </ul>
        <?php endif; ?>
    </div>
</div>
<div class="load">
    <?php echo $this->Html->image('../img/load.gif') ?>
</div>
<script type="text/javascript">
    $('.choose').click(function () {
        $('.load').fadeIn();
        var id = $(this).attr('value');
        var url = "<?php echo $this->Html->url(array('controller'=>'orders','action'=>'updatestatus')) ?>";
        var link = $(this).children('span').text();
        $.post(url, {id: id}, function (res) {
            $('.load').fadeOut();
            if (res == 'ok') {
//                $(this).attr('target', '_blank');
                alert('Chọn thành công!');
                window.open(link, '_blank');
                window.location.reload();
            } else {
                window.alert("Lỗi");
            }
        });
    });

    $('.unchoose').click(function () {
        $('.load').fadeIn();
        var id = $(this).attr('value');
//        alert(id);
        var url = "<?php echo $this->Html->url(array('controller'=>'orders','action'=>'unchoose')) ?>";
        $.post(url, {id: id}, function (res) {
            $('.load').fadeOut();
            if (res == 'ok') {
                alert('Bỏ chọn thành công!');
                window.location.reload();
            } else if (res == 'error') {
                window.alert("Lỗi");
            } else {
                alert('Order không tồn tại');
            }
        });
        return false;
    });
</script>